<?php
/**
 * 供应链模块
 */
namespace api\schain\controller;

use api\schain\model\SchainEnterpriseModel;
use cmf\controller\RestBaseController;

class EnterpriseController extends RestBaseController {

	protected $postModel;

	public function __construct(SchainEnterpriseModel $postModel) {
		parent::__construct();
		$this->postModel = $postModel;
	}

	/**
	 * [index description]
	 * @param int $page 分页参数
	 * @param int $limit 每页条数
	 * @return [type] [description]
	 */
	public function index() {
		$params = $this->request->get();

		$params['relation'] = 'user';

		$params['order'] = '-create_time';

		$data = $this->postModel->getDatas($params);

		foreach ($data as $key => $value) {
			$data[$key]['friendly_time'] = func_friendly_time($value['create_time']);

			//若果摘要为空，从内容中提取
			if (empty($value['ent_excerpt'])) {
				$data[$key]['ent_excerpt'] = str_replace("\n", "", strip_tags($value['ent_content']));
			}

			//获取行业
			$data[$key]['category'] = $value->categories()->alias('a')->column('a.name');
			$data[$key]['category_ids'] = $value->categories()->alias('a')->column('a.id');
		}

		if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
			$response = $data;
		} else {
			$response = ['list' => $data];
		}

		$this->success('请求成功!', $response);
	}

	/**
	 * 获取指定的文章
	 * @param int $id
	 */
	public function read() {
		$id = $this->request->get('id');

		if (intval($id) === 0) {
			$this->error('无效的文章id！');
		} else {
			$params = $this->request->get();
			$params['id'] = $id;
			$params['relation'] = 'user';
			$data = $this->postModel->getDatas($params);
			if (empty($data)) {
				$this->error('文章不存在！');
			} else {
				$this->postModel->where('id', $id)->setInc('ent_hits');

				$url = cmf_url('schain/Enterprise/index', ['id' => $id], true, true);

				//获取分类
				$category = $data->categories()->alias('a')->column('a.name');
				$category_ids = $data->categories()->alias('a')->column('a.id');

				$data = $data->toArray();
				$data['url'] = $url;
				$data['friendly_time'] = date('Y年m月d日 H:s', $data['create_time']);

				$data['ent_content'] = str_replace("\n", "<br>", strip_tags($data['ent_content'], "<img>"));

				if (substr($data['ent_content'], 0, 4) !== '<br>') {
					$data['ent_content'] = "<br>" . $data['ent_content'];
				}

				//LOGO
				if (!empty($data['more']['thumbnail'])) {
					$data['more']['thumbnail'] = cmf_get_image_url($data['more']['thumbnail']);
				}

				//相册
				if (!empty($data['more']['photos'])) {
					foreach ($data['more']['photos'] as $key => $value) {
						$data['more']['photos'][$key]['url'] = cmf_get_image_url($value['url']);
					}
				}

				//若果摘要为空，从内容中提取
				if (empty($data['ent_excerpt'])) {
					$data['ent_excerpt'] = str_replace("\n", "", strip_tags($data['ent_content']));
				}

				//获取行业
				$data['category'] = $category;
				$data['category_ids'] = $category_ids;

				$this->success('请求成功!', $data);
			}

		}
	}

	/**
	 * 名片搜索
	 * @return [type] [description]
	 */
	public function search() {
		$params = $this->request->get();
		if (!empty($params['keyword'])) {
			$params['where'] = [
				'ent_title|ent_summary|ent_location|ent_keywords|ent_content' => ['like', '%' . $params['keyword'] . '%'],
			];
			$data = $this->postModel->getDatas($params);

			foreach ($data as $key => $value) {
				$data[$key]['friendly_time'] = func_friendly_time($value['create_time']);

				//若果摘要为空，从内容中提取
				if (empty($value['ent_excerpt'])) {
					$data[$key]['ent_excerpt'] = str_replace("\n", "", strip_tags($value['ent_content']));
				}

				//若果昵称为空，用用户代表昵称
				if (empty($value['user']['user_nickent_title'])) {
					$data[$key]['user_nickent_title'] = empty($value['user']['user_login']) ? "未设置" : $value['user']['user_login'];
				} else {
					$data[$key]['user_nickent_title'] = $value['user']['user_nickent_title'];
				}

				//获取行业
				$data[$key]['category'] = $value->categories()->alias('a')->column('a.name');
				$data[$key]['category_ids'] = $value->categories()->alias('a')->column('a.id');
			}

			if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
				$response = $data;
			} else {
				$response = ['list' => $data];
			}

			$this->success('请求成功!', $response);
		} else {
			$this->error('搜索关键词不能为空！');
		}

	}

	// 文章点赞
	public function doLike() {
		$articleId = $this->request->param('id', 0, 'intval');

		$canLike = cmf_check_user_action("cards$articleId", 1);

		$this->postModel->where(['id' => $articleId])->setInc('ent_like');

		$likeCount = $this->postModel->where('id', $articleId)->value('ent_like');

		$this->success("赞好啦！", ['ent_like' => $likeCount]);
	}

	/**
	 * 分享名片
	 * @return [type] [description]
	 */
	public function doShare() {
		// $userId = $this->getUserId();
		$articleId = $this->request->param('id', 0, 'intval');

		$article = $this->postModel->where(['id' => $articleId])->field('id,ent_title,ent_content,more')->find();
		if (empty($article)) {
			$this->error('文章不存在！');
		}

		$this->postModel->where(['id' => $articleId])->setInc('ent_share');

		$shareCount = $this->postModel->where('id', $articleId)->value('ent_share');
		$this->success("分享成功！", ['ent_share' => $shareCount]);

	}

	/**
	 * 我的名片
	 * @return [type] [description]
	 */
	public function my() {
		$params = $this->request->get();
		$userId = $this->getUserId();
		var_dump($userId);
		$data = $this->postModel->getUserEnts($userId, $params);
		$this->success('请求成功!', $data);
	}
}
